package com.example.bbang.mapper;

import com.example.bbang.entity.Shenshu;
import com.example.bbang.entity.User;
import org.apache.ibatis.annotations.*;
import org.springframework.data.relational.core.sql.In;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface UserMapper {

    @Select("select * from user")
    List<User> findAll();
//根据用户名拿到自己的数据
    @Select("select * from user where username=#{username}")
    @Transactional
    List<User> findElseAll(@Param("username")String username);

   @Insert("INSERT INTO `user` ( `username`, `password`,`emil`,`status`,`img`) VALUES (#{username},#{password},#{emil},#{status},#{img})")
    @Transactional
   void save(User user);

//分页拿user
   @Select("select * from user limit #{pageNum},#{pageSize}")
    List<User> selectPage(@Param("pageNum") Integer pageNum, @Param("pageSize") Integer pageSize);



    @Update("<script> " +
            "update `user` " +
            "<set> " +
            "<if test = \"username != null and username!=''\">" +
            "username=#{username}," +
            "</if>" +
            "<if test = \"password != null and password!=''\">" +
            "password=#{password}," +
            "</if>" +
            "<if test = \"userTel != null and userTel!=''\">" +
            "userTel=#{userTel}," +
            "</if>" +
            "<if test = \"emil != null and emil!=''\">" +
            "emil=#{emil}," +
            "</if>" +
            "<if test = \"userSex != null and userSex!=''\">" +
            "userSex=#{userSex}," +
            "</if>" +
            "<if test = \"status != null and status!=''\">" +
            "status=#{status}," +
            "</if>" +
            "<if test = \"img != null and img!=''\">" +
            "img=#{img}," +
            "</if>" +
            "</set>" +
            " where userID = #{userID} " +
            "</script> ")
    @Transactional
    void updateById(User user);
    @Delete("delete from user where userID = #{userID}")
    void deleteById(Integer userID);

    @Select("select * from user where username=#{username} and password = #{password} and status=#{status}")
    User login(@Param("username") String username, @Param("password")String password ,@Param("status") Integer status);

    //查帮手
    @Select("select * from user where status = 1")
    List<User> findBsUser();


    //模搜分页拿用户
    @Select("select * from `user` where username like CONCAT('%',#{username},'%') and status = 1  limit #{pageNum},#{pageSize}")
    @Transactional
    List<User> findPageByUser(@Param("pageNum") Integer pageNum, @Param("pageSize") Integer pageSize, @Param("username") String username);

    //模搜分页拿帮手
    @Select("select * from `user` where username like CONCAT('%',#{username},'%') and status = 2  limit #{pageNum},#{pageSize}")
    @Transactional
    List<User> findPageByBSUser(@Param("pageNum") Integer pageNum, @Param("pageSize") Integer pageSize, @Param("username") String username);


    @Select("select * from `user` where username like CONCAT('%',#{username},'%')")
    List<User> ms(String username);

    @Update("update user set password=#{password} where username = #{username} ")
    @Transactional
    void updateByPassword(User user);


    @Select("SELECT COUNT(*) AS row_count FROM `user` where status = #{status}")
     Integer getUserCountByStatus(Integer status);

}
